<?php
namespace app\index\service;


use app\index\model\MainBasalListModel;
use app\index\model\MainBasalModel;
use app\index\model\MainEnginModel;
use app\index\model\MainHuishuModel;
use app\index\model\MainHuiziModel;
use app\index\model\MainJianChuiListModel;
use app\index\model\MainJianCuoListModel;
use app\index\model\MainJianEnginChuiModel;
use app\index\model\MainJianEnginCuoModel;
use app\index\model\MainJianEnginGongModel;
use app\index\model\MainJianEnginGuanModel;
use app\index\model\MainJianEnginYuoneModel;
use app\index\model\MainJianEnginYutwoModel;
use app\index\model\MainJianGongListModel;
use app\index\model\MainJianGuanListModel;
use app\index\model\MainMarkTypeModel;
use app\index\model\MainOptionModel;
use app\index\model\MainQingListModel;
use app\index\model\MainQingModel;
use app\index\model\UserModel;

class MainService extends CommonService
{


    public function get_one_baseinfo($num = '')
    {
        if(empty($num)){
            return [];
        }
        $topid = MainOptionModel::get_pk_by_num($this->userinfo['comid'], $num);

        $list = MainOptionModel::field('keyid as value,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'topid'=>$topid])->select()->toArray();
        return $list;
    }


    public function get_base_info($num = '')
    {
        if(empty($num)){
            return [];
        }
        $option_id = MainOptionModel::get_pk_by_num($this->userinfo['comid'], $num);
        if(!$option_id){
            return [];
        }
        return $this->get_option_all($option_id);
    }
    public function get_option_all($pid=0){
        $data = MainOptionModel::field('id,keyid as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
            ->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_option_all($item['id']);
        }
        return $data;
    }
    public function get_base_showname($num = '', $keyid)
    {
        if(empty($num) || empty($keyid)){
            return '';
        }
        $option_id = MainOptionModel::get_pk_by_num($this->userinfo['comid'], $num);
        $name = MainOptionModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'topid'=>$option_id, 'keyid'=>$keyid])->value('name');
        return $name;
    }
    public function get_optioninfo_all($pid=0){
        $data = MainOptionModel::field('*,id as value')->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_optioninfo_all($item['id']);
        }
        return $data;
    }
    public function check_option_name($name, $topid=0, $id=0){
        return !!MainOptionModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0, 'topid'=>$topid])
            ->where('id','neq', $id)->find();
    }

    public function check_option_keyid($keyid, $topid=0, $id=0){
        return !!MainOptionModel::where(['comid'=>$this->userinfo['comid'],'keyid'=>$keyid,'deleted_user'=>0, 'topid'=>$topid])
            ->where('id','neq', $id)->find();
    }

    public function get_editshi($date='', $user=''){
        $date = !$date ? date("Y-m-d H:i:s") : $date;
        $user = !$user ? $this->userinfo['id'] : $user;
        $name = UserModel::get_realname_by_pk($user);
        $str = '最后编辑：'.date("Y年m月d日H:i:s", strtotime($date)).' '.$name;
        return $str;
    }

    public function get_maineditshi($enginid, $type=''){
        $str = '';

        if(!empty($type)){
            $type = '_'.$type;
        }

        $upd_user = 0;
        $upd_at = '';
        $enginInfo = MainEnginModel::where(['id'=>$enginid])->find();
        if(!$enginInfo || !isset($enginInfo['updated_user'.$type]) || !isset($enginInfo['updated_at'.$type])){
            return $str;
        }else{
            $upd_user = $enginInfo['updated_user'.$type];
            $upd_at = $enginInfo['updated_at'.$type];
            if($upd_user < 1){
                return $str;
            }
        }

        $name = UserModel::get_realname_by_pk($upd_user);
        $str = '最后编辑：'.date("Y年m月d日H:i:s", strtotime($upd_at)).' '.$name;
        return $str;
    }




    public function get_main_jian_chui_list($paramid, $pid=0){
        $data = MainJianChuiListModel::field('*')
            ->where(['jianid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_chui_list($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_chui_list($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['jianid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['guige'] = $vvv['guige'];
            $ins['num'] = $vvv['num'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['jinfee'] = $vvv['jinfee'];
            $ins['jinallfee'] = $vvv['jinallfee'];
            $ins['jichufee'] = $vvv['jichufee'];
            $ins['jichuallfee'] = $vvv['jichuallfee'];
            $ins['yuzufee'] = $vvv['yuzufee'];
            $ins['zuhefee'] = $vvv['zuhefee'];
            $ins['zhigong'] = $vvv['zhigong'];
            $ins['zhiren'] = $vvv['zhiren'];
            $ins['sigong'] = $vvv['sigong'];
            $ins['siren'] = $vvv['siren'];
            $ins['zhihefee'] = $vvv['zhihefee'];
            $ins['qifee'] = $vvv['qifee'];
            $ins['allfee'] = $vvv['allfee'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianChuiListModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_chui_list($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_cuo_list($paramid, $pid=0){
        $data = MainJianCuoListModel::field('*')
            ->where(['cuoid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_cuo_list($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_cuo_list($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['cuoid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['guige'] = $vvv['guige'];
            $ins['num'] = $vvv['num'];
            $ins['price'] = $vvv['price'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianCuoListModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_cuo_list($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_gong_list($paramid, $pid=0){
        $data = MainJianGongListModel::field('*')
            ->where(['gongid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_gong_list($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_gong_list($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['gongid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['benfee'] = $vvv['benfee'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['gongfee'] = $vvv['gongfee'];
            $ins['shefee'] = $vvv['shefee'];
            $ins['hefee'] = $vvv['hefee'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianGongListModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_gong_list($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_guan_list($paramid, $pid=0){
        $data = MainJianGuanListModel::field('*')
            ->where(['guanid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_guan_list($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_guan_list($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['guanid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['num'] = $vvv['num'];
            $ins['price'] = $vvv['price'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianGuanListModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_guan_list($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }



    public function get_qing_list($paramid, $pid=0){
        $data = MainQingListModel::field('*')
            ->where(['qingid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_chui_list($paramid, $item['id']);
            }
        }
        return $data;
    }



    public function update_at_main($enginid, $type=''){
        $upd = [];
        if(!$type){
            $upd['updated_user'] = $this->userinfo['id'];
            $upd['updated_at'] = date("Y-m-d H:i:s");
        }else{
            $upd['updated_user_'.$type] = $this->userinfo['id'];
            $upd['updated_at_'.$type] = date("Y-m-d H:i:s");
        }
        return MainEnginModel::where(['id'=>$enginid])->update($upd);
    }





    public function get_main_type(){
        return [
            1 => '人工费',
            2 => '材料费',
            3 => '机械费',
            4 => '专业分包'
        ];
    }
    public function get_main_type_name($type){
        $arr = $this->get_main_type();
        $str = isset($arr[$type]) ? $arr[$type] : '';
        return $str;
    }


    public function create_dea_marktype($enginid){
        $data = [
            1 => 'basetype_ren',
            2 => 'basetype_cai',
            3 => 'basetype_ji',
            4 => 'basetype_fen'
        ];
        foreach ($data as $key=>$numName){
            $renId = MainOptionModel::where(['comid'=>$this->userinfo['comid'],'num'=>$numName])->value('id');
            $list = MainOptionModel::where(['comid'=>$this->userinfo['comid'],'pid'=>$renId])->order('sort ASC')->column('name');
            foreach($list as $k=>$nnn){
                MainMarkTypeModel::insert([
                    'comid'=>$this->userinfo['comid'],
                    'enginid'=>$enginid,
                    'type'=>$key,
                    'name'=>$nnn,
                    'sort' => $k + 1,
                ]);
            }
        }
        return true;
    }



    public function get_basal_nav_huizong($enginid, $enname){
        $where[] = ['enginid','=',$enginid];
        $item = [];
        $item['enname'] = $enname;
        $item['iszi'] = 1;
        // 1白色：未组价  2浅蓝色：已全部组价  3红色：部分组价  4深蓝色：已组价但包含成本价为0的项
        $item['iszhu'] = 1;
        $arr = MainBasalModel::where($where)->where(['fenname'=>$item['enname']])->field('id')->select()->toArray();
        $exist = 0; // 存在明细的数量
        $noexist = 0; // 不存在明细的数量
        $priceexist = 0; // 存在明细且包含0的数量
        foreach ($arr as $v) {
            $main_list = MainBasalListModel::where('basalid',$v['id'])->field('no_price')->select()->toArray();
            if($main_list){
                $exist += 1;
                foreach ($main_list as $vv){
                    if($vv['no_price'] == 0){
                        $priceexist += 1;
                    }
                }
            }else{
                $noexist += 1;
            }
        }
        if($exist == 0){
            $item['iszhu'] = 1;
        }
        if($exist != 0 && $noexist > 0){
            $item['iszhu'] = 3;
        }
        if($exist != 0 && $noexist == 0 && $priceexist == 0){
            $item['iszhu'] = 2;
        }
        if($exist != 0 && $noexist == 0 && $priceexist > 0){
            $item['iszhu'] = 4;
        }

        $item['enword'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('enword');
        $item['renprice'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('renprice');
        $item['caiprice'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('caiprice');
        $item['jiprice'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('jiprice');
        $item['fenprice'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('fenprice');
        $item['price'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('price');
        $item['money'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('money');
        $item['renfee'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('renfee');
        $item['caifee'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('caifee');
        $item['jifee'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('jifee');
        $item['fenfee'] = MainbasalModel::where($where)->where('fenname',$item['enname'])->sum('fenfee');

        $item['enword'] = number_format($item['enword'],2);
        $item['renprice'] = number_format($item['renprice'],2);
        $item['caiprice'] = number_format($item['caiprice'],2);
        $item['jiprice'] = number_format($item['jiprice'],2);
        $item['fenprice'] = number_format($item['fenprice'],2);
        $item['price'] = number_format($item['price'],2);
        $item['money'] = number_format($item['money'],2);
        $item['renfee'] = number_format($item['renfee'],2);
        $item['caifee'] = number_format($item['caifee'],2);
        $item['jifee'] = number_format($item['jifee'],2);
        $item['fenfee'] = number_format($item['fenfee'],2);
        return $item;
    }





    public function get_main_jian_engin_chui($paramid, $pid=0){
        $data = MainJianEnginChuiModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_chui($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_chui($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['guige'] = $vvv['guige'];
            $ins['num'] = $vvv['num'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['jinfee'] = $vvv['jinfee'];
            $ins['jinallfee'] = $vvv['jinallfee'];
            $ins['jichufee'] = $vvv['jichufee'];
            $ins['jichuallfee'] = $vvv['jichuallfee'];
            $ins['yuzufee'] = $vvv['yuzufee'];
            $ins['zuhefee'] = $vvv['zuhefee'];
            $ins['zhigong'] = $vvv['zhigong'];
            $ins['zhiren'] = $vvv['zhiren'];
            $ins['sigong'] = $vvv['sigong'];
            $ins['siren'] = $vvv['siren'];
            $ins['zhihefee'] = $vvv['zhihefee'];
            $ins['qifee'] = $vvv['qifee'];
            $ins['allfee'] = $vvv['allfee'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginChuiModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_chui($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_engin_cuo($paramid, $pid=0){
        $data = MainJianEnginCuoModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_cuo($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_cuo($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['guige'] = $vvv['guige'];
            $ins['num'] = $vvv['num'];
            $ins['price'] = $vvv['price'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginCuoModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_cuo($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_engin_gong($paramid, $pid=0){
        $data = MainJianEnginGongModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_gong($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_gong($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['benfee'] = $vvv['benfee'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['gongfee'] = $vvv['gongfee'];
            $ins['shefee'] = $vvv['shefee'];
            $ins['hefee'] = $vvv['hefee'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginGongModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_gong($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_engin_guan($paramid, $pid=0){
        $data = MainJianEnginGuanModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_guan($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_guan($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['num'] = $vvv['num'];
            $ins['price'] = $vvv['price'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginGuanModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_guan($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_engin_yuone($paramid, $pid=0){
        $data = MainJianEnginYuoneModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_yuone($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_yuone($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['num'] = $vvv['num'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['price'] = $vvv['price'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginYuoneModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_yuone($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }

    public function get_main_jian_engin_yutwo($paramid, $pid=0){
        $data = MainJianEnginYutwoModel::field('*')
            ->where(['enginid'=>$paramid,'pid'=>$pid])
            ->select()->toArray();
        foreach ($data as &$item){
            if($item['iscate'] > 0){
                $item['children'] = $this->get_main_jian_engin_yutwo($paramid, $item['id']);
            }
        }
        return $data;
    }
    public function add_main_jian_engin_yutwo($list, $mid, $pid=0){
        foreach($list as $vvv){
            $ins = [];
            $ins['enginid'] = $mid;
            $ins['pid'] = $pid;
            $ins['iscate'] = $vvv['iscate'];
            $ins['sorts'] = $vvv['sorts'];
            $ins['name'] = $vvv['name'];
            $ins['unit'] = $vvv['unit'];
            $ins['num'] = $vvv['num'];
            $ins['monnum'] = $vvv['monnum'];
            $ins['price'] = $vvv['price'];
            $ins['tanrate'] = $vvv['tanrate'];
            $ins['heprice'] = $vvv['heprice'];
            $ins['explain'] = $vvv['explain'];
            $zid = MainJianEnginYutwoModel::insertGetId($ins);
            if(isset($vvv['children'])){
                $this->add_main_jian_engin_yutwo($vvv['children'], $mid, $zid);
            }
        }
        return true;
    }


    public function create_huishu($enginid){
        $data = [
            ['name'=> '安装工程', 'info'=>'','chan'=>'[{"key":"qi","resvalue":"1","rate":"","ratio":""}]'],
            ['name'=> '垂直运输及进出场安拆费', 'info'=>'','chan'=>'[{"key":"jian","resvalue":"chui","rate":"","ratio":""}]'],
            ['name'=> '措施费（安全文明施工费）', 'info'=>'','chan'=>'[{"key":"jian","resvalue":"cuo","rate":"","ratio":""}]'],
            ['name'=> '项目管理费成本', 'info'=>'','chan'=>'[{"key":"jian","resvalue":"gong","rate":"","ratio":""},{"key":"jian","resvalue":"guan","rate":"","ratio":""}]'],
            ['name'=> '预置表1', 'info'=>'','chan'=>'[{"key":"jian","resvalue":"yuzhi","rate":"","ratio":""}]'],
            ['name'=> '预置表2', 'info'=>'','chan'=>'[{"key":"jian","resvalue":"yuzhitwo","rate":"","ratio":""}]'],
        ];

        foreach ($data as $item){
            $item['enginid'] = $enginid;
            MainHuishuModel::insert($item);
        }
        return true;
    }

    public function get_qingArr($enginid){
        $data = [];
        $list = MainQingModel::where(['enginid'=>$enginid])->order('sort ASC')->order('id ASC')->column('id');
        foreach ($list as $qingid){
            $qinglist = MainqinglistModel::where(['enginid'=>$enginid,'qingid'=>$qingid])->select()->toArray();
            $zongfee = 0;
            foreach ($qinglist as $v){
                $zongfee += $v['money'];
            }
            $data[$qingid] = $zongfee;
        }
        return $data;
    }
    public function get_jianArr($enginid){
        $data['chui'] = MainJianEnginChuiModel::where(['enginid'=>$enginid,'pid'=>0])->sum('allfee');
        $data['cuo'] = MainJianEnginCuoModel::where(['enginid'=>$enginid,'pid'=>0])->sum('heprice');
        $data['gong'] = MainJianEnginGongModel::where(['enginid'=>$enginid,'pid'=>0])->sum('hefee');
        $data['guan'] = MainJianEnginGuanModel::where(['enginid'=>$enginid,'pid'=>0])->sum('heprice');
        $data['yuzhi'] = MainJianEnginYuoneModel::where(['enginid'=>$enginid,'pid'=>0])->sum('heprice');
        $data['yuzhitwo'] = MainJianEnginYutwoModel::where(['enginid'=>$enginid,'pid'=>0])->sum('heprice');
        return $data;
    }
    public function get_huishu_chan($enginid, $chanstr, $qingArr, $jianArr){
        $fee = 0;
        $arr = json_decode($chanstr, true);
        if(!$arr){
            return $fee;
        }

        foreach ($arr as $item){
            if($item['key'] == 'qing'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $zongfee = isset($qingArr[$item['resvalue']]) ? $qingArr[$item['resvalue']] : 0;
                $fee += $zongfee * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'jian'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $mm = isset($jianArr[$item['resvalue']]) ? $jianArr[$item['resvalue']] : 0;
                $fee += $mm * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'tou'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $hui_hanfee = MainEnginModel::where(['id'=>$enginid])->value('money');
                $fee += $hui_hanfee * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'zijin'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $dianli = $this->get_dianli($enginid);
                $fee += $dianli * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'qi'){
                $item['rate'] = empty($item['rate']) ? 0 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $fee += maths(substr($item['rate'],  1));
                }else{
                    $fee += $item['rate'];
                }
            }
        }

        return $fee;
    }
    public function get_dianli($enginid)
    {
        $jianArr = $this->get_jianArr($enginid);
        $qingArr = $this->get_huizi_qingArr($enginid);
        $list = MainHuiziModel::where(['enginid'=>$enginid])->select()->toArray();
        $dianli = 0;
        foreach ($list as $key=>&$item){
            $item['chanfee'] = $this->get_huizi_chan($item['enginid'], $item['chan'], $qingArr,$jianArr);
            $item['shoufee'] = $this->get_huizi_chan($item['enginid'], $item['shou'], $qingArr,$jianArr);
            $item['zhifee'] = $item['chanfee'] * $item['zhirate']/100;

            if($key == 0){
                $item['leichan'] = $item['chanfee'];
            }else{
                $item['leichan'] = $item['chanfee'] + $list[$key-1]['leichan'];
            }

            if($key == 0){
                $item['leishou'] = $item['shoufee'];
            }else{
                $item['leishou'] = $item['shoufee'] + $list[$key-1]['leishou'];
            }


            if($key == 0){
                $item['leizhi'] = $item['zhifee'];
            }else{
                $item['leizhi'] = $item['zhifee'] + $list[$key-1]['leizhi'];
            }
            $item['dianzhi'] = $item['leishou'] - $item['leizhi'];
            $dianli += -($item['dianzhi'] * $item['dianrate']/100);
        }

        return $dianli;
    }
    public function get_huizi_qingArr($enginid){
        $data = [];
        $list = MainQingModel::where(['enginid'=>$enginid])->column('id');
        foreach ($list as $qingid){
            $qingArr = [];
            $qinglist = MainQingListModel::where(['enginid'=>$enginid,'qingid'=>$qingid])->select()->toArray();

            foreach ($qinglist as $v){

                if(isset($qingArr[$v['fenname']])){
                    $qingArr[$v['fenname']] += $v['money'];
                    $qingArr[$v['fenname'].'_ren'] += $v['renfee_cur'];
                    $qingArr[$v['fenname'].'_cai'] += $v['caifee_cur'];
                    $qingArr[$v['fenname'].'_ji'] += $v['jifee_cur'];
                    $qingArr[$v['fenname'].'_fen'] += $v['fenfee_cur'];
                }else{
                    $qingArr[$v['fenname']] = $v['money'];
                    $qingArr[$v['fenname'].'_ren'] = $v['renfee_cur'];
                    $qingArr[$v['fenname'].'_cai'] = $v['caifee_cur'];
                    $qingArr[$v['fenname'].'_ji'] = $v['jifee_cur'];
                    $qingArr[$v['fenname'].'_fen'] = $v['fenfee_cur'];
                }
            }
            $data[$qingid] = $qingArr;
        }
        return $data;
    }
    public function get_huizi_chan($enginid, $chanstr, $qingArr, $jianArr){
        $fee = 0;
        $arr = json_decode($chanstr, true);
        if(!$arr){
            return $fee;
        }

        foreach ($arr as $item){
            if($item['key'] == 'qing'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                if(isset($item['fenname']) && !is_array($item['fenname'])){
                    $item['fenname'] = json_decode($item['fenname'], true);
                }

                if(isset($item['fenname']) && is_array($item['fenname'])){
                    $fenfee = 0;
                    foreach ($item['fenname'] as $kk=>$vv){
                        $vv['rate'] = empty($vv['rate']) ? 1 : $vv['rate'];
                        if(substr($vv['rate'], 0 , 1) === '='){
                            $vv['rate'] = maths(substr($vv['rate'],  1));
                        }
                        if(isset($qingArr[$item['resvalue']][$vv['fenname']])){
                            $fenfee += $qingArr[$item['resvalue']][$vv['fenname']] * $vv['rate'];
                        }
                    }
                    $fee += $fenfee * $item['rate'] * $item['ratio'];
                }
            }
            if($item['key'] == 'jian'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $mm = isset($jianArr[$item['resvalue']]) ? $jianArr[$item['resvalue']] : 0;
                $fee += $mm * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'tou'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['ratio'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $hui_hanfee = MainEnginModel::where(['id'=>$enginid])->value('money');
                $fee += $hui_hanfee * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'qi'){
                $item['rate'] = empty($item['rate']) ? 0 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $fee += maths(substr($item['rate'],  1));
                }else{
                    $fee += $item['rate'];
                }
            }
        }

        return $fee;
    }
    public function get_huizao_type(){
        return [1=>'劳务费', 2=>'分包', 3=>'机械费', 4=>'材料费'];
    }
    public function get_huizao_chengfee($info, $basalfee){
        $chengArr = json_decode($info['cheng'], true);
        if(!$chengArr){
            $chengArr = [];
        }
        $chengfee = 0;
        foreach ($chengArr as $kk=>$vv){
            $arr = explode(',', $vv);
            foreach ($arr as $name){
                $chengfee += isset($basalfee[$kk][$name]) ? $basalfee[$kk][$name] : 0;
            }
        }
        return $chengfee;
    }
    public function get_huizao_basalfee($enginid){
        $data = [1=>[],2=>[],3=>[],4=>[]];
        $nameArr = MainBasalModel::field('fenname')->where(['enginid'=>$enginid])->group('fenname')->order('id ASC')->column('fenname');
        foreach ($nameArr as $fenname){
            $cai = 0;
            $ren = 0;
            $zhuan = 0;
            $ji = 0;
            $info = MainBasalModel::where(['enginid'=>$enginid])->where(['fenname'=>$fenname])->order('id ASC')->select()->toArray();
            foreach ($info as $vvv){
                $ren += $vvv['renfee'];
                $cai += $vvv['caifee'];
                $ji += $vvv['jifee'];
                $zhuan += $vvv['fenfee'];
            }
            $data[1][$fenname] = $ren;
            $data[2][$fenname] = $zhuan;
            $data[3][$fenname] = $ji;
            $data[4][$fenname] = $cai;
        }
        return $data;
    }
    public function get_chan_list($enginid, $chanStr, $qingArr, $jianArr)
    {
        $list = [];
        $arr = json_decode($chanStr, true);
        if(!$arr){
            return $list;
        }

        foreach ($arr as $item){
            if($item['key'] == 'qing'){
                $money = 0;
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                if(isset($item['fenname']) && !is_array($item['fenname'])){
                    $item['fenname'] = json_decode($item['fenname'], true);
                }

                if(isset($item['fenname']) && is_array($item['fenname'])){
                    $fenfee = 0;
                    foreach ($item['fenname'] as $kk=>$vv){
                        $vv['rate'] = empty($vv['rate']) ? 1 : $vv['rate'];
                        if(substr($vv['rate'], 0 , 1) === '='){
                            $vv['rate'] = maths(substr($vv['rate'],  1));
                        }
                        // 分部向数据
                        $list[$vv['fenname']] = 0;
                        $list[$vv['fenname'].'_ren'.$item['resvalue']] = 0;
                        $list[$vv['fenname'].'_cai'.$item['resvalue']] = 0;
                        $list[$vv['fenname'].'_ji'.$item['resvalue']] = 0;
                        $list[$vv['fenname'].'_fen'.$item['resvalue']] = 0;

                        $list[$vv['fenname'].'_ren'] = 0;
                        $list[$vv['fenname'].'_cai'] = 0;
                        $list[$vv['fenname'].'_ji'] = 0;
                        $list[$vv['fenname'].'_fen'] = 0;
                        if(isset($qingArr[$item['resvalue']][$vv['fenname']])){
                            $fenfee += $qingArr[$item['resvalue']][$vv['fenname']] * $vv['rate'];

                            $list[$vv['fenname']] = $qingArr[$item['resvalue']][$vv['fenname']] * $vv['rate'];
                            $list[$vv['fenname'].'_ren'.$item['resvalue']] = $qingArr[$item['resvalue']][$vv['fenname'].'_ren'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_cai'.$item['resvalue']] = $qingArr[$item['resvalue']][$vv['fenname'].'_cai'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_ji'.$item['resvalue']] = $qingArr[$item['resvalue']][$vv['fenname'].'_ji'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_fen'.$item['resvalue']] = $qingArr[$item['resvalue']][$vv['fenname'].'_fen'] * $vv['rate'] * $item['rate'] * $item['ratio'];

                            $list[$vv['fenname'].'_ren'] += $qingArr[$item['resvalue']][$vv['fenname'].'_ren'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_cai'] += $qingArr[$item['resvalue']][$vv['fenname'].'_cai'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_ji'] += $qingArr[$item['resvalue']][$vv['fenname'].'_ji'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                            $list[$vv['fenname'].'_fen'] += $qingArr[$item['resvalue']][$vv['fenname'].'_fen'] * $vv['rate'] * $item['rate'] * $item['ratio'];
                        }
                    }
                    $money += $fenfee * $item['rate'] * $item['ratio'];
                }
                $list[$item['resvalue']] = $money;
            }
            if($item['key'] == 'jian'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $money = isset($jianArr[$item['resvalue']]) ? $jianArr[$item['resvalue']] : 0;
                $money = $money * $item['rate'] * $item['ratio'];
                $list[$item['resvalue']] = $money;
            }
            if($item['key'] == 'tou'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['ratio'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                $hui_hanfee = MainEnginModel::where(['id'=>$enginid])->value('money');
                $money = $hui_hanfee * $item['rate'] * $item['ratio'];
                $list['tou'] = $money;
            }
            if($item['key'] == 'qi'){
                $item['rate'] = empty($item['rate']) ? 0 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $money = maths(substr($item['rate'],  1));
                }else{
                    $money = $item['rate'];
                }
                $list[$item['resvalue']] = $money;
            }
        }

        return $list;
    }
    public function get_shou_money($enginid, $shouStr, $chanArr, $key)
    {
        $fee = 0;
        $arr = json_decode($shouStr, true);
        if(!$arr){
            return $fee;
        }

        $qiMoney = 0;
        foreach ($arr as $item){
            if($item['key'] == 'qing'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }

                // 获取列数据
                $arr = array_column($chanArr, (int)$item['resvalue']);
                $money = array_sum($arr);
                if(empty($money)) $money = 0;
                $fee += $money * $item['rate'] * $item['ratio'];

            }
            if($item['key'] == 'jian'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }
                // 获取列数据
                $arr = array_column($chanArr, $item['resvalue']);
                $money = array_sum($arr);
                if(empty($money)) $money = 0;
                $fee += $money * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'tou'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                if(substr($item['ratio'], 0 , 1) === '='){
                    $item['ratio'] = maths(substr($item['ratio'],  1));
                }
                // 获取列数据
                $arr = array_column($chanArr, 'tou');
                $money = array_sum($arr);
                if(empty($money)) $money = 0;
                $fee += $money * $item['rate'] * $item['ratio'];
            }
            if($item['key'] == 'qi'){
                $item['rate'] = empty($item['rate']) ? 0 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $money = maths(substr($item['rate'],  1));
                    $qiMoney += maths(substr($item['rate'],  1));
                }else{
                    $money = $item['rate'];
                    $qiMoney += $item['rate'];
                }
                $fee += $money;
            }
        }

        return [$fee,$qiMoney];
    }
    public function get_zhi_money($enginid, $zhiStr, $chanArr, $key)
    {
        $fee = 0;
        $arr = json_decode($zhiStr, true);
        if(!$arr){
            return $fee;
        }
        $upfee = 0;
        foreach ($arr as $item){
            if($item['key'] == 'qing'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }

                if(isset($item['fenname']) && !is_array($item['fenname'])){
                    $item['fenname'] = json_decode($item['fenname'], true);
                }


                if(isset($item['fenname']) && is_array($item['fenname'])){
                    $upfee = 0;
                    foreach ($item['fenname'] as $kk=>$vv){
                        $vv['rate_ren'] = empty($vv['rate_ren']) ? 0 : $vv['rate_ren'];
                        $vv['rate_cai'] = empty($vv['rate_cai']) ? 0 : $vv['rate_cai'];
                        $vv['rate_ji'] = empty($vv['rate_ji']) ? 0 : $vv['rate_ji'];
                        $vv['rate_fen'] = empty($vv['rate_fen']) ? 0 : $vv['rate_fen'];
                        if(substr($vv['rate_ren'], 0 , 1) === '='){
                            $vv['rate_ren'] = maths(substr($vv['rate_ren'],  1));
                        }
                        if(substr($vv['rate_cai'], 0 , 1) === '='){
                            $vv['rate_cai'] = maths(substr($vv['rate_cai'],  1));
                        }
                        if(substr($vv['rate_ji'], 0 , 1) === '='){
                            $vv['rate_ji'] = maths(substr($vv['rate_ji'],  1));
                        }
                        if(substr($vv['rate_fen'], 0 , 1) === '='){
                            $vv['rate_fen'] = maths(substr($vv['rate_fen'],  1));
                        }

                        $arr = array_column($chanArr, $vv['fenname'].'_ren'.$item['resvalue']);
                        $money = array_sum($arr);
                        if(empty($money)) $money = 0;
                        $fee += $money * $vv['rate_ren'];

                        $arr = array_column($chanArr, $vv['fenname'].'_cai'.$item['resvalue']);
                        $money = array_sum($arr);
                        if(empty($money)) $money = 0;
                        $fee += $money * $vv['rate_cai'];

                        $arr = array_column($chanArr, $vv['fenname'].'_ji'.$item['resvalue']);
                        $money = array_sum($arr);
                        if(empty($money)) $money = 0;
                        $fee += $money * $vv['rate_ji'];

                        $arr = array_column($chanArr, $vv['fenname'].'_fen'.$item['resvalue']);
                        $money = array_sum($arr);
                        if(empty($money)) $money = 0;
                        $fee += $money * $vv['rate_fen'];
                    }
                }
            }
            if($item['key'] == 'jian'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                // 获取列数据
                $arr = array_column($chanArr, $item['resvalue']);
                $money = array_sum($arr);
                if(empty($money)) $money = 0;
                $fee += $money * $item['rate'];
            }
            if($item['key'] == 'tou'){
                $item['rate'] = empty($item['rate']) ? 1 : $item['rate'];
                $item['ratio'] = empty($item['ratio']) ? 1 : $item['ratio'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $item['rate'] = maths(substr($item['rate'],  1));
                }
                // 获取列数据
                $arr = array_column($chanArr, 'tou');
                $money = array_sum($arr);
                if(empty($money)) $money = 0;
                $fee += $money * $item['rate'];
            }
            if($item['key'] == 'qi'){
                $item['rate'] = empty($item['rate']) ? 0 : $item['rate'];
                if(substr($item['rate'], 0 , 1) === '='){
                    $money = maths(substr($item['rate'],  1));
                }else{
                    $money = $item['rate'];
                }
                $fee += $money;
            }
        }

        return $fee;
    }
    public function get_huizi_zhiqingArr($enginid, $fenname){
        $data = [];
        $list = MainQingModel::where(['enginid'=>$enginid])->order('sort ASC')->order('id ASC')->column('id');
        foreach ($list as $qingid){
            $qingArr = [];
            $qinglist = MainQingListModel::where(['enginid'=>$enginid,'qingid'=>$qingid,'fenname'=>$fenname])->order('id ASC')->select()->toArray();

            foreach ($qinglist as $v){
                if(isset($qingArr[$v['fenname']])){
                    $qingArr[$v['fenname']]['money'] += $v['money'];
                    $qingArr[$v['fenname']]['renfee'] += $v['renfee_cur'];
                    $qingArr[$v['fenname']]['caifee'] += $v['caifee_cur'];
                    $qingArr[$v['fenname']]['jifee'] += $v['jifee_cur'];
                    $qingArr[$v['fenname']]['fenfee'] += $v['fenfee_cur'];
                }else{
                    $qingArr[$v['fenname']]['money'] = $v['money'];
                    $qingArr[$v['fenname']]['renfee'] = $v['renfee_cur'];
                    $qingArr[$v['fenname']]['caifee'] = $v['caifee_cur'];
                    $qingArr[$v['fenname']]['jifee'] = $v['jifee_cur'];
                    $qingArr[$v['fenname']]['fenfee'] = $v['fenfee_cur'];
                }
            }
            $data[$qingid] = $qingArr;
        }
        return $data;
    }




    /**
     * 导出数据
     * @param $list //表格数据二维数组
     * @param $excelFieldsZHCN //表头字段二维数组
     * @param $excelFileName //Excel文件名字字符串
     * @param $sheetTitle    //Excel文件sheet名字一维数组
     */
    function exportExcels($list, $excelFieldsZHCN, $excelFileName, $sheetTitle)
    {
        $excelFileName = iconv('UTF-8', 'GBK', $excelFileName);
//        $excelFileName = iconv('UTF-8', 'UTF-8', $excelFileName);
        $excelFileName = $excelFileName;
        require_once '../extend/phpoffice/PHPExcel/PHPExcel.php';
        $objPHPExcel = new \PHPExcel();
        $index = 0;
        $ret = [];
        foreach ($excelFieldsZHCN as $key => $value) {
            $objPHPExcel->createSheet();
            $i = 0;
            foreach ($value as $fieldName => $ZHCN) {
                $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                $objPHPExcel->getActiveSheet()->getColumnDimension($pCoordinate)->setWidth(30);
                $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . '1', $value[$fieldName]);
                $objPHPExcel->getActiveSheet()->getStyle($pCoordinate . '1')->getFont()->setBold(true);
                $ret[$i] = $fieldName;
                $i++;
            }
            $row = 2;
            if ($list[$key]) {
                foreach ($list[$key] as $kk=>$item) {
                    $i = 0;
                    foreach ($ret as $field) {
                        $pCoordinate = \PHPExcel_Cell::stringFromColumnIndex($i);
                        if(!isset($item[$field])){
                            $item[$field] = '';
                        }
                        $objPHPExcel->setActiveSheetIndex($index)->setCellValue($pCoordinate . $row, '' . $item[$field]);
                        $i++;
                    }
                    $objPHPExcel->getActiveSheet()->getStyle('C'.($kk+2))->getAlignment()->setWrapText(true);
                    $row++;
                }
            }
            $objPHPExcel->getActiveSheet()->setTitle($sheetTitle[$key]);
            $objPHPExcel->setActiveSheetIndex($index);
            $datacouns = count($list[$key])+2;
            $objPHPExcel->getActiveSheet()->getStyle('A1:z'.$datacouns)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中
            $index++;
        }
        $objPHPExcel->removeSheetByIndex(count($sheetTitle));

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $excelFileName . '.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: cache, must-revalidate');
        header('Pragma: public');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('mainexcel/'.$excelFileName.'.xls');
    }




}

